package com.example.practice2023.service;

import com.example.practice2023.mapper.GoodMapper;
import com.example.practice2023.pojo.Good;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodService {
    @Resource
    private GoodMapper goodMapper;
    public Map<String,Object> selectById(Integer goodId){
        Good good=goodMapper.selectById(goodId);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "基于主键id查询成功");
        map.put("data", good);
        return map;
    }

    public Map<String,Object> selectByPage(int page,int limit,Good good){   //Good good
        int index = (page - 1) * limit;   // 通用性的分页起始下标计算
        List<Good> goodList = goodMapper.selectByPage(index, limit,
                good.getGoodTitle(),
                good.getGoodCode(),
                good.getGoodType(),
                good.getGoodStatus());    //, good.getAge(), good.getSex()
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", goodMapper.selectAllCount(
                good.getGoodTitle(),
                good.getGoodCode(),
                good.getGoodType(),
                good.getGoodStatus()));   // user.getAge(), user.getSex()  通过数据库全查功能，把所有的记录个数统计出来
        map.put("data", goodList);
        return map;
    }

    public Map<String,Object> insert(Good good){
        Map<String, Object> map = new HashMap<>();
        goodMapper.insert(good);
        map.put("code", 200);
        map.put("msg", "新增成功");
        map.put("data", good);   // user.getAge(), user.getSex()  通过数据库全查功能，把所有的记录个数统计出来
//        map.put("data", goodList);
        return map;
    }
    public Map<String,Object> updateById(Good good){
        Map<String, Object> map = new HashMap<>();
        goodMapper.updateById(good);
        map.put("code", 200);
        map.put("msg", "更新成功");
        map.put("data", good);   // user.getAge(), user.getSex()  通过数据库全查功能，把所有的记录个数统计出来
//        map.put("data", goodList);
        return map;
    }
    public Map<String,Object> deleteById(Integer goodId){
        Map<String, Object> map = new HashMap<>();
        goodMapper.deleteById(goodId);
        map.put("code", 200);
        map.put("msg", "删除成功");
        map.put("id", goodId);   // user.getAge(), user.getSex()  通过数据库全查功能，把所有的记录个数统计出来
//        map.put("data", goodList);
        return map;
    }
}
